package dk.pervasivepositioning.project2;

import java.io.IOException;
import java.util.Timer;

import android.app.Activity;
import android.content.Context;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Project2Activity extends Activity implements
		OnItemSelectedListener {
	/** Called when the activity is first created. */

	Context context = this;
	LocationListener listener;
	LocationManager lm;
	Timer t = new Timer();
	ReportingStrategy strategy;
	private final String[] options = { "periodic", "distanced-based",
			"configurable-maximum-speed", "accelerometer based with threshold",
			"distanced-max-speed-accelerometer" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);
		Spinner spinner = (Spinner) findViewById(R.id.spinner1);
		spinner.setAdapter(new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_dropdown_item, options));
		spinner.setOnItemSelectedListener(this);

		lm = (LocationManager) context.getSystemService(LOCATION_SERVICE);
	}

	public void startService(View button) throws IOException {
		try {
			startService();
		} catch (Exception e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
			e.printStackTrace();
		}
	}

	public void startService() throws IOException {
		Spinner spinner = (Spinner) findViewById(R.id.spinner1);
		String selectedItem = (String) spinner.getSelectedItem();
		String URL = ((EditText) findViewById(R.id.editText1)).getText()
				.toString();

		if (selectedItem.equals("distanced-based")) {
			int threshold = Integer
					.valueOf(((EditText) findViewById(R.id.editText2))
							.getText().toString());
			strategy = new DistancedBasedStrategy(URL, lm, threshold, this);
		} else if (selectedItem.equals("configurable-maximum-speed")) {
			int threshold = Integer
					.valueOf(((EditText) findViewById(R.id.editText2))
							.getText().toString());
			double maximumSpeed = Double
					.valueOf(((EditText) findViewById(R.id.editText3))
							.getText().toString());
			strategy = new SmartDistanceBasedStrategy(URL, lm, threshold,
					maximumSpeed, this);
		} else if (selectedItem.equals("distanced-max-speed-accelerometer")) {
			int threshold = Integer
					.valueOf(((EditText) findViewById(R.id.editText2))
							.getText().toString());
			int maximumSpeed = Integer
					.valueOf(((EditText) findViewById(R.id.editText3))
							.getText().toString());
			strategy = new SmartDistancedBasedWithAcc(URL, lm, threshold, this,
					maximumSpeed);
		} else if (selectedItem.equals("accelerometer based with threshold")) {
			int threshold = Integer
					.valueOf(((EditText) findViewById(R.id.editText2))
							.getText().toString());
			strategy = new AccelerometerBased(URL, lm, this, threshold);
		} else {
			EditText timePeriodText = (EditText) findViewById(R.id.editText2);
			int timeperiod = Integer.valueOf(timePeriodText.getText()
					.toString());
			strategy = new PeriodicStrategy(URL, timeperiod, lm, this);
		}
		strategy.startup();
		findViewById(R.id.setup_id).setVisibility(View.GONE);
		findViewById(R.id.running_id).setVisibility(View.VISIBLE);

	}

	public void stopService(View button) {
		findViewById(R.id.setup_id).setVisibility(View.VISIBLE);
		findViewById(R.id.running_id).setVisibility(View.GONE);
		((TextView) findViewById(R.id.gps_fixes)).setText("GPS FIXES: 0");
		TextView text = (TextView) findViewById(R.id.what_is_happening);
		text.setText("");
		strategy.shutdown();
	}

	@Override
	public void onBackPressed() {
		View view = findViewById(R.id.running_id);
		if (view.getVisibility() == View.VISIBLE) {
			stopService(view);
		} else {
			super.onBackPressed();
		}
	}

	public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
			long id) {
		TextView textViewToChange = (TextView) findViewById(R.id.textView3);
		String s;
		int visibilityTextView;
		int visibilityEditText;
		String selected = options[position];
		if (selected.equals("configurable-maximum-speed")
				|| selected.equals("distanced-max-speed-accelerometer")) {
			s = "Threshold";
			visibilityEditText = View.VISIBLE;
			visibilityTextView = View.VISIBLE;
		} else if (selected.equals("distanced-based")
				|| selected.equals("accelerometer based with threshold")) {
			visibilityTextView = View.GONE;
			visibilityEditText = View.GONE;
			s = "Threshold (m)";
		} else {
			s = "Updating frequency (ms)";
			visibilityTextView = View.GONE;
			visibilityEditText = View.GONE;
		}

		textViewToChange.setText(s);
		findViewById(R.id.textView4).setVisibility(visibilityEditText);
		findViewById(R.id.editText3).setVisibility(visibilityTextView);
	}

	public void onNothingSelected(AdapterView<?> arg0) {
	}

}